#ifndef SERVICEMANAGER_H
#define SERVICEMANAGER_H
#include <map>
#include "Singleton.h"

class Service;
class User;

typedef std::map<DWORD, Service*>		SERVICE_MAP;
typedef SERVICE_MAP::iterator			SERVICE_MAP_ITER;
typedef std::pair<DWORD, Service*>		SERVICE_MAP_PAIR;

class ServiceManager : public Singleton<ServiceManager>
{
public:
	ServiceManager()
	{
        memset(m_Services,0,sizeof(m_Services));
	}
	~ServiceManager()
	{
         SERVICE_MAP_ITER iter = NULL;
		 for (iter = m_mapServices.begin();iter!=m_mapServices.end();iter++)
		 {
             delete iter->second;
		 }
	}
	Service* GetService(DWORD dwIndex)
	{   
		return m_Services[dwIndex];
	}

	Service* FindService(DWORD dwHash)
	{   
		if(m_mapServices.find(dwHash)!=m_mapServices.end())
		{
             return m_mapServices[dwHash];
		}
		else
		{
             return NULL;
		}
	}

	BOOL  AddService(DWORD dwHash,Service* pService)
	{
         if(m_mapServices.find(dwHash)==m_mapServices.end())
		 {
              m_mapServices.insert(SERVICE_MAP_PAIR(dwHash,pService));
			  return TRUE;
		 }
		 else
		 {
			 return FALSE;
		 }
	}

	Service*  DeleteService(DWORD dwHash)
	{   
		SERVICE_MAP_ITER Iter = m_mapServices.find(dwHash);
		if(Iter!=m_mapServices.end())
		{
			m_mapServices.erase(Iter);
			return Iter->second;
			
		}
		else
		{
			return NULL;
		}
           
	}

private:
	SERVICE_MAP						m_mapServices;
	Service*                        m_Services[255];          

};


#endif